<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\bootstrap\ActiveForm;
use yii\widgets\LinkPager;
$this->title = '评价管理';
?>
<div class="container">
	<div class="row">
		<?php echo $this->render('_leftnav') ?>
		<div class="col-md-10">
			<div class="page-header clearfix">
				<h1 class="pull-left">课程评价</h1>
				<div class="pull-right">
				</div>
			</div>

			<?php 
				$form = ActiveForm::begin([
					'options' => ['class' => 'form-inline well well-sm',
					'id'=>'comment-search-form'],
					'action'=>'/index.php?r=course/evaluate',
					'method'=>'post',
					'fieldConfig' => [
						'template' => '<div class="form-group"><div class="col-md-8 controls"  style="padding-top: 9px">{input}{error}</div></div>'
					], 
				]);
			?>
				<div class="form-group">
					<?= Html::input('text', 'course_name', $search['course_name'], ['class' => 'form-control','placeholder' => '课程名']) ?>
				</div>

				<div class="form-group">
					<?= $form->field($model, 'user_name',['inputOptions'=>['placeholder'=>'作者','value'=>$search['user_name']]])->textInput() ?>
				</div>

				<div class="form-group">
					<?= $form->field($model, 'comment_content',['inputOptions'=>['placeholder'=>'评价内容关键词','value'=>$search['comment_content']]])->textInput() ?>
				</div>
				<?= Html::submitButton('搜索', ['class' => 'btn btn-primary','id'=>'comment-search-btn']) ?>
			<?php ActiveForm::end(); ?> 
			<div id="review-table-container">
				<table class="table table-striped table-hover" id="review-table">
					<thead>
						<tr>
							<th width="4%">
								<?= Html::checkbox('checkboxlist', null,['id' => 'alls','data-role' => 'batch-select','value' => '']) ?>
							</th>
							<th width="40%">评价内容</th>
							<th width="20">审核状态</th>
							<th width="26%">作者</th>
							<th width="10%">操作</th>
						</tr>
					</thead>
					<tbody>
						<?php if(empty($data)){?>
							<tr>
								<td colspan="20">
									<div class="empty">暂无评价记录</div>
								</td>
							</tr>
						<?php }else{?>
						<?php foreach($data as $k=>$v){?>
							<tr id="review-table-tr-2" data-role="item">
								<td>
									<?= Html::checkbox('checkboxlist', null,['class' => 'che','data-role' => 'batch-item','value' => $v['comment_id']]) ?>
								</td>
								<td>
									<div class="short-long-text">
										<div class="short-text text-sm text-muted <?php echo 'a'.$v['comment_id']?>" style="display:block" id="<?php echo 'a'.$v['comment_id']?>"><?= Html::encode(mb_substr($v['comment_content'],0,40,'utf-8'))?>...<span class="trigger unfold" id="<?php echo $v['comment_id']?>">(展开)</span>
										</div>
										<div class="long-text <?php echo $v['comment_id']?>" style="display:none" >
											<p>
												<span style="color:rgb(102,102,102);"><?= Html::encode($v['comment_content'])?>
												</span>
											</p> 
											<span class="trigger packup" aa=<?php echo 'a'.$v['comment_id']?>>(收起)
											</span>
										</div>
									</div>
									<div class="mts">
										<?php foreach($v['comment'] as $kk=>$vv){?>
											<a class="text-success text-sm" href="#" target="_blank"><?php echo $vv['course_name']?></a>
										<?php }?>
									</div>
								</td>
								<td align="centent">
								<?php 
									if($v['comment_status']==1){
										echo "<a href='javascript:;' class='pass' type='".$v['comment_status']."' id='".$v['comment_id']."'><font color='green'>通过</font></a>";
									}else{
										echo "<a href='javascript:;' class='nopass' type='".$v['comment_status']."' id='".$v['comment_id']."'><font color='red'>不通过</font></a>";
									}
									?>
								</td>
								<td>
									<a href="javascript:;" role="show-user" data-toggle="modal" data-target="#modal" data-url="http://www.web.cc/Yii/edusoho/web/app.php/admin/user/1"><?php echo $v['user_name']?></a>
									<br>
									<span class="text-muted"><?php echo date("Y-m-d H:s:i",$v['comment_addtime'])?></span>
								</td>
								<td>
									<button class="btn btn-default btn-sm deletecomment" type="<?php echo $v['comment_id']?>">删除</button>
								</td>
							</tr>
						<?php }?>
					<?php }?>
					</tbody>
				</table>
				
				<div>
					<label class="checkbox-inline">
						<?= Html::checkbox('checkboxlist', null,['id' => 'all','value' => '']) ?>
					全选</label>
					<button class="btn btn-default btn-sm mlm deletecomments" >删除</button>
				</div>
				<div align="right">
						<?php echo LinkPager::widget([
							'pagination' => $pagination,
							'firstPageLabel'=>"首页",
							'prevPageLabel'=>'上一页',
							'nextPageLabel'=>'下一页',
							'lastPageLabel'=>'尾页',
						]);?>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
	$(".deletecomment").click(function(){
		if(confirm("您确定要删除这条记录？")){
			var comment_id=$(this).attr("type");
			$.ajax({
				type: "POST",
				url: "/index.php?r=course/commentdelete",
				data: "comment_id="+comment_id,
				success: function(msg){
					if(msg==1){
						alert("删除成功")
						history.go(0)
					}else{
						alert("请求失败")
					}
				}
			});
		}
	})
	//全选
	$(document).on("click","#all",function(){
		if($(this).prop("checked")==true){
			$(".che").prop("checked",true);
			$("#alls").prop("checked",true);
		}else{
			$(".che").prop("checked",false);
			$("#alls").prop("checked",false);
		}
	});
	//全选
	$(document).on("click","#alls",function(){
		if($(this).prop("checked")==true){
			$(".che").prop("checked",true);
			$("#all").prop("checked",true);
		}else{
			$(".che").prop("checked",false);
			$("#all").prop("checked",false);
		}
	});
	//批量操作
	$(".deletecomments").click(function(){
		if(confirm("您确定要执行此操作")){
			var valArr = ""; 
			$(".che").each(function(){
				if($(this).prop("checked")==true){
					valArr += ","+$(this).val();
				}
			});
			//var valArr = valArr.join(',');//转换为逗号隔开的字符串 
			var comment_id=valArr.substr(1);
			$.ajax({
				type: "POST",
				url: "/index.php?r=course/commentdelete",
				data: "comment_id="+comment_id,
				dataType: 'json',
				success: function(msg){
					if (msg.code == 200) {
						history.go(0)
					}else if(msg.code == 400)
					{
						alert('删除失败')
					}else if(msg.code == 403) 
					{
						alert('暂无权限')
					}
				}
			});
		}
	})
	//通过
	$(document).on("click",".pass",function(){
		var comment_status = $(this).attr("type");
		var comment_id = $(this).attr("id");
		var self=$(this);
		$.ajax({
			type: "POST",
			url: "/index.php?r=course/commentupdate",
			data: "comment_status="+comment_status+"&comment_id="+comment_id,
			success: function(msg){
				if(msg==0){
					self.replaceWith("<a href='javascript:;' class='nopass' type='0' id='"+comment_id+"'><font color='red'>不通过</font></a>")
				}else if(msg==1){
					self.replaceWith("<a href='javascript:;' class='pass' type='1' id='"+comment_id+"'><font color='green'>通过</font></a>")
				}else{
					alert("请求失败")
				}
			}
		});
	});
	//不通过
	$(document).on("click",".nopass",function(){
		var comment_status = $(this).attr("type");
		var comment_id = $(this).attr("id");
		var self=$(this);
		$.ajax({
			type: "POST",
			url: "/index.php?r=course/commentupdate",
			data: "comment_status="+comment_status+"&comment_id="+comment_id,
			success: function(msg){
				if(msg==0){
					self.replaceWith("<a href='javascript:;' class='nopass' type='0' id='"+comment_id+"'><font color='red'>不通过</font></a>")
				}else if(msg==1){
					self.replaceWith("<a href='javascript:;' class='pass' type='1' id='"+comment_id+"'><font color='green'>通过</font></a>")
				}else{
					alert("请求失败")
				}
			}
		});
	});
	//点击展开
	$(".unfold").click(function(){
		var id=$(this).attr("id");
		var aid=$(this).parent().attr("id");
		$("."+id).css("display","block");
		$("."+aid).css("display","none");
	})

	//点击收起
	$(".packup").click(function(){
		var id=$(this).attr("aa");
		$(".long-text").css("display","none");
		$("."+id).css("display","block");
	})
</script>
